<div class="weui-flex__item">
    <div class="weui-cells__title">我的证件照正面</div>
    <div class="weui-cell">
        <div class="weui-cell__bd">
            <div class="weui-uploader">
                <div class="weui-uploader__bd">
                    <div class="weui-cell__bd">
                        <img id="face-img" class="weui-uploader__file" src="" alt="">
                    </div>
                    <div class="weui-uploader__input-box">
                        <input id="document-photo-up-a" autocomplete='off' class="weui-uploader__input" type="file" a1ccept="image/*" multiple="" ／>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="weui-flex__item">
    <div class="weui-cells__title">我的证件照背面</div>
    <div class="weui-cell">
        <div class="weui-cell__bd">
            <div class="weui-uploader">
                <div class="weui-uploader__bd">
                    <div class="weui-cell__bd">
                        <img id="back-img" class="weui-uploader__file" src="" alt="">
                    </div>
                    <div class="weui-uploader__input-box">
                        <input id="document-photo-up-b" autocomplete='off' class="weui-uploader__input" type="file" a1ccept="image/*" multiple="" ／>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    (function (w, u) {
        /**
         * 上传图片到服务器
         * @param inputID 上传的输入框ID
         * @param uri 上传的地址
         * @return Promise
         * */

        var  uploadImg = function (inputID, uri) {
            return new Promise(function(resolve, reject) {
                $(inputID).on('change', function () {
                    var formDate = new FormData();
                    formDate.append('0', $(inputID)[0].files[0]);
                    $.ajax({
                        url: uri,
                        type: 'POST',
                        cache: false,
                        data: formDate,
                        processData: false,
                        contentType: false,
                        success: function (data) {
                            resolve(data)
                        },
                        error: function (err) {
                            reject(err)
                        }
                    })
                })
            })
        };
        var faceImg = $('#face-img');
        var backImg = $('#back-img');
        // 上传身份证正面
        uploadImg('#document-photo-up-a', 'http://api.hongyanche.com/upload/idPhoto').then(function(data)  {
            if (data.state == 1) {
                $.ajax({
                    type: 'POST',
                    url: '/user/photo_add',
                    data: {
                        url: data.data.url,
                        type: 1
                    },
                    success: function (e) {
                        console.log('上传成功' + e);
//                        let datas = data.data.info;
//                        console.log('--------------------------------------');
//                        console.log(data.data)
                        if(e.state) {
                            faceImg.attr('src', data.data.url);
                        }else{
                            TOAST.warn(e.errormsg, ' ', 2);
                        }
                    }
                })
            }
        }).catch(function(err) {
            console.log(err);
            TOAST.warn("上传图片失败,请重试", ' ', 2);
        });

        // 上传身份证背面
        uploadImg('#document-photo-up-b', 'http://api.hongyanche.com/upload/idPhoto').then(function(data){
            if (data.state == 1) {
                $.ajax({
                    type: 'POST',
                    url: '/user/photo_add',
                    data: {
                        url: data.data.url,
                        type: 0
                    },
                    success: function (e) {
                        console.log('上传成功' + e);
                        backImg.attr('src', data.data.url);
                    },
                    error: function (err) {
                        TOAST.warn("上传失败", ' ', 2);
                    }
                })
            }
        }).catch(function(err) {
            console.log(err);
        TOAST.warn("上传失败.请重试", ' ', 2);
        });

        var us = '/user/photo_info';


        $.ajax({
            type: 'GET',
            url: us,
            async: true,
            success: function (data) {
                if(data.state) {
                    var datas = data.data.info;
                    faceImg.attr('src', datas.id_photo_face);
                    backImg.attr('src', datas.id_photo_back);
                }else{
                    TOAST.warn(data.errormsg, ' ', 2);
                }
            }
        })
    })(window, undefined);
</script>